package cc.geguo.settree.iteratordemo;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;

/**
 * 作者：13687
 * 日期：2017/6/18
 * 项目：JavaSetTree
 * 描述：使用迭代器操作 List 集合中的数据
 * 1. List 中能插入null 吗
 *
 */
public class IteratorTest {
	public static void main(String[] args) {
		concurrentModify();
//		concurrentRemove();
		linkedListTest();
	}



	private static void concurrentModify() {
		List<String > list = new ArrayList<>();
		list.add("test1");
		list.add("test4");
		list.add("test3");

/*			Iterator<String> it = list.iterator();

	while (it.hasNext()){
//			System.out.println(it.next());
			if ("test4".equals(it.next())){
				list.add("error");// java.util.ConcurrentModificationException
//				System.out.println(it.next());
			}
		}
		*/

		ListIterator<String> it = list.listIterator();

		while (it.hasNext()){
			if ("test4".equals(it.next())){
				it.add("error");// java.util.ConcurrentModificationException
			}
		}
		System.out.println("list:"+list);
	}
	private static void linkedListTest() {
		LinkedList linkedList = new LinkedList();
		linkedList.addFirst("hello");
		linkedList.pop();// 弹出来
	}

	private static void concurrentRemove() {
		List<String > list = new ArrayList<>();
		list.add("test1");
		list.add("test4");
		list.add("test3");

		Iterator<String> it = list.iterator();

		while (it.hasNext()){
			if ("test4".equals(it.next())){
//				it.next();// 每次调用指针都会往后移动
			    it.remove(); // 移除的是当前
			}
		}

		System.out.println("list:"+list);

	}

	private static void VectorTest(){
		Vector vector = new Vector();
		vector.add("test");
		Enumeration iterator = vector.elements();
	}
}
